package com.szl.yeb.controller;

import com.szl.minio.service.MinioService;
import com.szl.yeb.common.constant.MsgConstant;
import com.szl.yeb.common.R;
import com.szl.yeb.dto.AdminPasswordInfo;
import com.szl.yeb.entity.Admin;
import com.szl.yeb.exception.CustomException;
import com.szl.yeb.service.AdminService;
import com.szl.yeb.utils.ResponseProcessor;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.UUID;

/**
 * @program: yeb
 * @description: 操作员个人中心服务接口
 * @author: szl
 * @create: 2022-09-03 13:51
 **/
@Api(tags = "操作员个人中心服务接口(AdminInfoController)")
@RestController
public class AdminInfoController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private MinioService minioService;

    @PutMapping("/admin/info")
    @ApiOperation("更新当前登录操作员信息")
    public R<Object> updateCurrentAdmin(@RequestBody Admin admin, Authentication authentication) {
        boolean result = adminService.updateById(admin);
        SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(admin, null, authentication.getAuthorities()));
        return ResponseProcessor.getResWithMsg(result, MsgConstant.UPDATE);
    }

    @PutMapping("/admin/pass")
    @ApiOperation("更新当前登录操作员登录密码")
    public R<Object> updateCurrentAdminOnPassword(@RequestBody AdminPasswordInfo adminPasswordInfo) {
        return ResponseProcessor.getResWithMsg(adminService.updateCurrentAdminOnPassword(adminPasswordInfo), MsgConstant.UPDATE);
    }

    @PostMapping("/admin/userface")
    @ApiOperation("更新当前登录操作员头像")
    public R<Object> updateCurrentAdminOnUserFace(MultipartFile file, Authentication authentication, HttpServletResponse response) {

        if (file.isEmpty() || file.getSize() == 0) {
            throw new CustomException("文件为空");
        }
        String fileName = file.getOriginalFilename();
        assert fileName != null;
        String newName = "image/" + UUID.randomUUID().toString().replaceAll("-", "")
                + fileName.substring(fileName.lastIndexOf("."));

        String url = null;
        try {

            InputStream inputStream = file.getInputStream();
            //上传文件到Minio
            minioService.uploadFile(inputStream, newName, file.getContentType());

            url = minioService.getFilePermanentUrl(newName);

        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException("头像上传失败");
        }
        return ResponseProcessor.getResWithMsg(adminService.updateCurrentAdminOnUserFace(url,authentication,response), MsgConstant.UPDATE);
    }
}
